"""
Wordcraft:ClassDef.Block\n
/Code/ClassDef_Block.py\n
方块类
"""
from Code.ClassDef_Enums import BlockMaterial, NormalItemMaterial
from typing import *


class Block:
    """
    Wordcraft:ClassDef.Block.Block\n
    方块类\n
    """

    def __init__(self, type_id: str, character: str, can_dig: bool, material: Union[BlockMaterial, int],
                 hardness: int, level: Union[NormalItemMaterial, int], give: List[Union[List, Tuple]],
                 can_make: bool, how_to_make: Union[List, Tuple], color: Union[List, Tuple]):

        self.TypeID: str = type_id
        self.Character: str = character
        self.CanDig: bool = can_dig
        self.Material: BlockMaterial = material
        self.Hardness: int = hardness
        self.Level: NormalItemMaterial = level
        self.Give: List[Union[List, Tuple]] = give
        self.CanMake: bool = can_make
        self.HowToMake: Union[Tuple, List] = how_to_make
        self.Color = color

    def to_dict(self):
        return {
            "TypeID": self.TypeID,
            "Character": self.Character,
            "CanDig": self.CanDig,
            "Material": self.Material,
            "Hardness": self.Hardness,
            "Level": self.Level,
            "Give": self.Give,
            "CanMake": self.CanMake,
            "HowToMake": self.HowToMake,
            "Color": self.Color
        }

    @classmethod
    def from_dict(cls, d: dict):
        return cls(
            d["TypeID"],
            d["Character"],
            d["CanDig"],
            d["Material"],
            d["Hardness"],
            d["Level"],
            d["Give"],
            d["CanMake"],
            d["HowToMake"],
            d["Color"]
        )

    @classmethod
    def builtin(cls, type_id: str, log: bool = True):
        """
        Wordcraft:ClassDef.Block.Block.builtin\n
        内置方块\n
        参数：\n
        type_id：方块类型ID\n
        log：是否输出日志\n
        """
        import json
        with open("blocksconfig.json", "r", encoding="UTF-8") as f:
            blocks = json.load(f)
            try:
                cls = cls.from_dict(blocks[type_id])
            except KeyError:
                import Code.System_Index as System
                if log:
                    System.Out.Log.Error(System.Out.LangText(
                        "ERROR_CLASSDEF_BLOCK_BUILTIN_NAMENOTFOUND_PART1")+type_id+System.Out.LangText(
                            "ERROR_CLASSDEF_BLOCK_BUILTIN_NAMENOTFOUND_PART2"),
                        message="ERROR_CLASSDEF_BLOCK_BUILTIN_NAMENOTFOUND", target=type_id)
                exit()
        return cls
